<template>
    <div class="order-report">
        <div class="report-table-top-title">商家列表</div>
        <div class="report-table-form1">
            <div class="report-table-form1-line">
                <div
                    class="report-table-form1-item word samll"
                    style="width: 70px"
                >
                    商家编号:
                </div>
                <div class="report-table-form1-item" style="width: 180px">
                    <el-input
                        v-model="searchNo"
                        placeholder
                        size="small"
                        suffix-icon="el-icon-search"
                    ></el-input>
                </div>
                <div
                    class="report-table-form1-item word samll"
                    style="width: 70px"
                >
                    商家名称:
                </div>
                <div class="report-table-form1-item" style="width: 180px">
                    <el-input
                        v-model="searchName"
                        placeholder
                        size="small"
                        suffix-icon="el-icon-search"
                    ></el-input>
                </div>
                <div style="margin-top: 20px">
                    <div
                        class="report-table-form1-item word samll"
                        style="width: 70px"
                    >
                        注册时间:
                    </div>
                    <div class="report-table-form1-item">
                        <el-date-picker
                            v-model="beginTime"
                            type="date"
                            style="width: 180px"
                            size="small"
                            value-format="yyyy-MM-dd"
                            format="yyyy-MM-dd"
                            placeholder="选择日期时间"
                        >
                        </el-date-picker>

                        <span>至:</span>
                        <el-date-picker
                            v-model="endTime"
                            style="width: 180px"
                            type="date"
                            size="small"
                            value-format="yyyy-MM-dd"
                            format="yyyy-MM-dd"
                            placeholder="选择日期时间"
                        >
                        </el-date-picker>
                    </div>
                    <el-button type="primary" size="small" @click="search"
                        >搜索</el-button
                    >
                    <!--<div class="report-table-form1-item" style="width: 100px">-->
                    <!--</div>-->
                    <el-button type="primary" size="small" @click="toAdd"
                        >新增商家</el-button
                    >
                    <el-button type="primary" size="small" @click="exportRport"
                        >导出Excel</el-button
                    >
                </div>
            </div>
        </div>
        <div class="page-padding-gray-small"></div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="showList"
                ref="agentTable"
                border
                style="width: 100%"
            >
                <el-table-column
                    prop="companyCode"
                    label="商家编号"
                ></el-table-column>
                <el-table-column
                    prop="companyName"
                    label="商家名称"
                    show-overflow-tooltip
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="createtime"
                    label="注册时间"
                    min-width="100"
                >
                    <template slot-scope="props">
                        <div>{{ $FS(props.row.createtime) }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    v-if="managementType !== 3"
                    prop="expiredTime"
                    label="软件过期时间"
                    min-width="100"
                >
                    <template slot-scope="props">
                        <div v-show="!$IN7DAY(props.row.expiredTime)">
                            {{ $FS(props.row.expiredTime) }}
                        </div>
                        <div
                            v-show="$IN7DAY(props.row.expiredTime)"
                            style="color: red"
                        >
                            {{ $FS(props.row.expiredTime) }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    v-if="managementType !== 3 && managementType !== 4"
                    prop="roomPoint"
                    label="卡台数"
                    min-width="65"
                ></el-table-column>
                <el-table-column
                    prop="netPayChannel"
                    label="支付通道"
                    min-width="70"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.netPayChannel === 1">娱加</div>
                        <div v-if="scope.row.netPayChannel === 2">K米</div>
                        <div v-if="scope.row.netPayChannel === 3">
                            原生微信/支付宝
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="netPayRate"
                    label="支付费率(单位/千分)"
                    min-width="70"
                >
                    <template slot-scope="scope">
                        {{
                            scope.row.netPayChannel === 2 ||
                            scope.row.netPayChannel === 3
                                ? '-'
                                : scope.row.netPayRate
                        }}
                    </template>
                </el-table-column>
                <!-- <el-table-column prop="equipmentPoint" label="设备数" min-width="65"></el-table-column> -->
                <el-table-column
                    prop="addServiceCompanyList"
                    label="增值服务列表"
                    show-overflow-tooltip
                    min-width="100"
                >
                    <template slot-scope="props">
                        <span
                            v-for="(item, index) in props.row
                                .addServiceCompanyList"
                            :key="index"
                            >{{ item.addServiceName }},</span
                        >
                    </template>
                </el-table-column>
                <el-table-column
                    v-if="managementType !== 3 && managementType !== 4"
                    prop="temporaryExpiredTime"
                    label="临时限制"
                    min-width="90"
                >
                    <template slot-scope="props">
                        <div>{{ $FS(props.row.temporaryExpiredTime) }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="primary"
                            @click="toModify(scope.$index, scope.row)"
                            >修改</el-button
                        >
                          <el-dropdown trigger="click">
                            <el-button type="primary" size="mini">
                                更多操作<i
                                    class="el-icon-arrow-down el-icon--right"
                                ></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item @click.native="resetPassword(scope.row)" v-if="roleId === 2 && managementType !== 3">重置密码</el-dropdown-item>
                                <el-dropdown-item @click.native="toNetPayChannel(scope.$index, scope.row)">网络支付配置</el-dropdown-item>
                                <el-dropdown-item @click.native="toBuyService(scope.$index, scope.row)">购买增值服务</el-dropdown-item>
                                <el-dropdown-item @click.native="toServiceList(scope.$index, scope.row)" v-if="false">增值服务列表</el-dropdown-item>
                                <el-dropdown-item @click.native="toLimit(scope.row)" v-if="managementType !== 3 && managementType !== 4">使用临时限制</el-dropdown-item>
                                <el-dropdown-item @click.native="toHistory(scope.$index, scope.row)">历史</el-dropdown-item>
                                <el-dropdown-item @click.native="getVersionList(scope.$index, scope.row)" v-if="managementType !== 3">指定apk版本</el-dropdown-item>
                                <el-dropdown-item @click.native="projectorEdition(scope.$index, scope.row)" >指定皮肤包版本</el-dropdown-item>
                                <el-dropdown-item @click.native="toWareInput(scope.$index, scope.row)">支付进件</el-dropdown-item>
                                <el-dropdown-item @click.native="toEdition(scope.$index, scope.row)" >查看版本</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                    </template>
                </el-table-column>
                <el-table-column type="expand" v-if="managementType !== 3">
                    <template slot-scope="props">
                        <div style="line-height: 18px; font-size: 16px">
                            <div>联系人：{{ props.row.contact }}</div>
                            <div>联系方式：{{ props.row.telPhone }}</div>
                            <div v-show="props.row.managementType === 3">
                                总店编号：{{ props.row.mainCompanyCode }}
                            </div>
                            <div>
                                经营类型：{{
                                    props.row.managementTypeName
                                }}（有效期至{{ $F(props.row.expiredTime) }}）
                            </div>
                            <div>
                                商家地址：{{ props.row.provincialLevelName }}省
                                {{ props.row.cityLevelName }}市
                                {{ props.row.address }}
                            </div>
                            <div>备注：{{ props.row.remark }}</div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <el-dialog title="使用临时限制" :visible.sync="showLimit">
            <el-form size="small" label-width="240px">
                <el-form-item label="软件注册有效期至:">
                    <el-date-picker
                        size="small"
                        v-model="registExpiredTime"
                        type="date"
                        disabled
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="软件使用截止至:">
                    <el-date-picker
                        size="small"
                        v-model="temporaryExpiredTime"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期"
                    ></el-date-picker>
                </el-form-item>
                <el-form-item label="信息提示:">
                    <span style="color: red"
                        >软件在使用截止时间前7天开始提醒客户交尾款，截止时间到则软件暂停使用。</span
                    >
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="small" @click="setLimit"
                        >保存</el-button
                    >
                    <el-button
                        type="primary"
                        plain
                        size="small"
                        @click="showLimit = false"
                        >取消</el-button
                    >
                </el-form-item>
            </el-form>
        </el-dialog>

        <div class="report-table-block">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="10"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>

        <div class="page-padding"></div>
        <projectorDialog
            :currRow="currRow"
            :value.sync="isShowProjector"
            @getPage="getPage"
        ></projectorDialog>
        <appointAPK
            :currRow="currRow"
            :value.sync="showChangeVersion"
            @getPage="getPage"
        ></appointAPK>
    </div>
</template>

<script type="text/ecmascript-6">
import fileDownload from 'js-file-download'
/* 引入增值服务 */
import axios from 'axios'
import projectorDialog from './components/projectorDialog'
import appointAPK from './components/appointAPK'
export default {
    data () {
        return {
            isShowAppoint: false,
            isShowEdition: false,
            showChangeVersion: false,
            editVersionCompanyId: '',
            editVersionList: [],

            showLimit: false, // 显示临时限制
            limitRow: {},
            registExpiredTime: '', // 软件注册有效期至
            temporaryExpiredTime: '', // 软件有效期至

            nowPage: 0, // 当前所在页面

            showList: [],
            /* 增值服务列表 */
            addservicecompanylist: [],
            totalNum: 0,
            position: 1, // 页码

            beginTime: '',
            endTime: '',
            searchNo: '',
            searchName: '',
            addServiceList: '',
            options4: [],
            currRow: {},
            isShowProjector: false
        }
    },
    mounted () {
    },
    created () {
         this.getPage()
    },
    watch: {
        position () {
            this.getPage()
        }
    },
    components: {
        projectorDialog,
        appointAPK
    },
    props: {
        underLine: null,
        managementType: Number
    },
    computed: {
        roleId () {
            return this.$store.state.roleId
        }
    },
    methods: {
        projectorEdition (index, row) {
            this.isShowProjector = true
            this.currRow = row
        },
        showEdition (index, row) {
            this.isShowEdition = true
            this.currRow = row
        },
        resetPassword (row) {
            this.$confirm('确定重置密码为 syclub ?', '重置密码', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post(this.$store.state.IP + '/system/company/resetCompanyPsw', {
                    data: {
                        companyCode: row.companyCode,
                        managementType: this.managementType
                    }
                },
                    {
                        'emulateJSON': false
                    }
                ).then(response => {
                    response = response.body
                    console.log(response)
                    if (response.ret === '0') {
                        this.$message({
                            showClose: true,
                            message: '重置成功',
                            type: 'success'
                        })
                    }
                }, response => {
                })
            }).catch(() => {
            })
        },
        /* handleCommand (command) {
            console.log(command)
            if (command.action === 'addPoint') {
                if (command.obj.supportExtandPoint === 1) {
                    if (command.obj.roomPoint >= command.obj.authMaxPoint) {
                        this.$message({
                            showClose: true,
                            message: '该商家加点已满',
                            type: 'warning'
                        })
                        return
                    }
                }
                if (command.obj.supportExtandPoint === 0) {
                    if (command.obj.roomPoint >= command.obj.maxPoint) {
                        this.$message({
                            showClose: true,
                            message: '该商家加点已达上限',
                            type: 'warning'
                        })
                        return
                    }
                }
                this.toAddPoint(command.index, command.obj)
            }
            if (command.action === 'addDate') {
                // 创建时间和过期时间判断是否超过十年
                if (this.$overTenYear(command.obj.createtime, command.obj.expiredTime)) {
                    this.$message({
                        showClose: true,
                        message: '该商家续期已达上限',
                        type: 'warning'
                    })
                    return
                }
                this.toAddDate(command.index, command.obj)
            }
            if (command.action === 'addStore') {
                if (command.obj.memoryCloudsLenght === 365) {
                    this.$message({
                        showClose: true,
                        message: '该商家云存储已满',
                        type: 'warning'
                    })
                    return
                }
                this.toAddStore(command.index, command.obj)
            }
        }, */
        moveButtonWrapper (obj, action, index) {
            return {
                obj: obj,
                action: action,
                index: index
            }
        },

        getSellClass () {
            this.$http.post(this.$store.state.IP + '/system/company/getManagementTypeDict', {
                data: {
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用获取经营类型列表成功')
                    console.log(response.data)
                    this.options4 = [{
                        key: 0,
                        value: '全部'
                    }]
                    response.data.forEach(item => {
                        this.options4.push(item)
                    })
                }
                if (response.ret !== '0') {
                    console.log('调用获取经营类型列表失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },
        toLimit (row) {
            this.limitRow = row
            this.showLimit = true
            this.registExpiredTime = row.expiredTime ? this.copyArr(this.$FS(row.expiredTime) + ' 00:00:00') : ''
            this.temporaryExpiredTime = row.temporaryExpiredTime ? this.copyArr(this.$FS(row.temporaryExpiredTime) + ' 00:00:00') : ''
            console.log(this.registExpiredTime)
            console.log(this.temporaryExpiredTime)
        },
        setLimit () {
            this.$http.post(this.$store.state.IP + '/system/company/optTemporaryExpireTime', {
                data: {
                    temporaryExpiredTime: this.temporaryExpiredTime,
                    companyID: this.limitRow.companyId
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('设置临时限制成功')
                    console.log(response.data)
                    this.showLimit = false
                    this.$message({
                        showClose: true,
                        message: '临时限制设置成功',
                        type: 'success'
                    })
                    this.getPage()
                }
                if (response.ret !== '0') {
                    console.log('设置临时限制失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },
        getVersionList (index, row) {
            this.currRow = row
            this.showChangeVersion = true
        },
        changeVersion (index, row) {
            this.$http.post(this.$store.state.IP + '/system/company/setSpecifiedVersion', {
                data: {
                    companyid: this.editVersionCompanyId,
                    specifiedVersionCode: row.versionCode
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('指定apk版本信息成功')
                    console.log(response.data)
                    this.showChangeVersion = false
                    this.getPage()
                }
                if (response.ret !== '0') {
                    console.log('指定apk版本信息失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },

        search () {
            if (this.endTime) {
                if (this.endTime < this.beginTime) {
                    return this.$message({
                        message: '开始时间不能大于结束时间',
                        type: 'warning'
                    })
                }
            }
            this.position = 1
            this.getPage()
        },
        toAdd () {
            this.$store.dispatch('setKA', 1)
        },
        toModify (index, row) {
            this.$store.dispatch('setKA', 2)
            this.$store.dispatch('setKAE', row.companyId)
        },
        toHistory (index, row) {
            this.$store.dispatch('setKA', 3)
            this.$store.dispatch('setKAH', row.companyCode)
        },
        toAddPoint (index, row) {
            this.$store.dispatch('setKA', 4)
            this.$store.dispatch('setKAE', row.companyId)
        },
        toAddDate (index, row) {
            this.$store.dispatch('setKA', 5)
            this.$store.dispatch('setKAE', row.companyId)
        },
        toAddStore (index, row) {
            this.$store.dispatch('setKA', 6)
            this.$store.dispatch('setKAE', row.companyId)
        },
        toServiceList (index, row) {
            this.$store.dispatch('setKA', 7)
            this.$store.dispatch('setKAS', row.companyCode)
        },
        toBuyService (index, row) {
            this.$store.dispatch('setKA', 8)
            this.$store.dispatch('setKAS', row.companyCode)
            this.$store.dispatch('setKAE', row.companyId)
            this.$store.dispatch('setKAName', row.companyName)
            this.$store.dispatch('setKAManagementType', row.managementType)
        },
        toWareInput (index, row) {
            this.$store.dispatch('setKA', 9)
            this.$store.dispatch('setKAE', row.companyId)
            this.$store.dispatch('setKAS', row.companyCode)
        },
        toEdition (index, row) {
            this.$store.dispatch('setKA', 12)
            this.$store.dispatch('setCurrRow', row)
        },
        toNetPayChannel (index, row) {
            // 网络支付配置
            this.$store.dispatch('setKA', 13)
            this.$store.dispatch('setKAE', row.companyId)
            this.$store.dispatch('setKMCompanyName', row.companyName)
            this.$store.dispatch('setKAManagementType', row.managementType)
        },
        exportRport () {
            if (this.endTime) {
                if (this.endTime < this.beginTime) {
                    return this.$message({
                        message: '开始时间不能大于结束时间',
                        type: 'warning'
                    })
                }
            }
            let that = this
            let endTime = this.endTime ? this.endTime + ' 23:59:59' : ''
            let beginTime = this.beginTime ? this.beginTime + ' 00:00:00' : ''
            axios.get(this.$store.state.IP + '/server/serverorder/exportOrderExcel' +
                '?companyCode=' + this.searchNo +
                '&companyName=' + this.searchName +
                '&managementType=' + this.managementType +
                '&endTime=' + endTime +
                '&beginTime=' + beginTime,
                {
                    responseType: 'blob',
                    headers: that.axiosDownloadHeaders()
                }
            ).then(function (response) {
                console.log(response)
                if (response.headers['content-type'] === 'application/json;charset=UTF-8') {
                    console.log('出错')
                    return
                }
                let fileName = decodeURIComponent(response.headers['content-disposition'])
                fileName = fileName.substring(20)
                if (response.headers['content-type'] === 'application/x-msdownload;charset=UTF-8') {
                    fileDownload(response.data, fileName)
                }
            }).catch(function (error) {
                alert(error)
            })
        },

        getPage () {
            this.$http.post(this.$store.state.IP + '/system/company/page', {
                data: {
                    page: this.position - 1, // 页数
                    limit: 10, // 一页显示数量
                    companyCode: this.searchNo,
                    beginTime: this.beginTime ? this.beginTime + ' 00:00:00' : '',
                    endTime: this.endTime ? this.endTime + ' 23:59:59' : '',
                    companyName: this.searchName,
                    managementType: this.managementType
                }
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    console.log('调用出品详情成功')
                    console.log(response.data)
                    this.showList = response.data.list
                    // this.addservicecompanylist = response.data.list.addservicecompanylist
                    this.totalNum = response.data.total
                    // let self = this
                }
                if (response.ret !== '0') {
                    console.log('调用出品详情失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },

        // 换页方法
        handleCurrentChange (val) {
            this.position = val
        },
        // 数据重新上传K米
        refreshKmData (index, row) {
            console.log(row)
            let companyIDs = []
            companyIDs.push(row.companyId)
            this.$http.post(this.$store.state.IP + '/kmshop/refresh', {
                companyIDs: companyIDs
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                console.log('-------------------------------------------')
                response = response.body
                console.log(response)
                if (response.ret === '0') {
                    this.$message({
                        showClose: true,
                        message: '数据重新上传K米成功',
                        type: 'success'
                    })
                }
                if (response.ret !== '0') {
                    console.log('调用数据重新上传K米失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('调用数据重新上传K米失败')
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-form1
        width: 100%
        min-height: 40px
        padding: 16px 0 0 22px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align: left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff

.btns
    margin-right: 10px
    margin-bottom: 10px
    margin-left: 0 !important
</style>
